package co.edu.usbcali.raju.db;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class RajuConnection {
	
	private Connection conn;
	private String dbms;
	public String jarFile;
	public String dbName; 
	public String userName;
	public String password;
	public String urlString;

	private String driver;
	private String serverName;
	private int portNumber;
	private Properties prop;
	
	public static RajuConnection instance;

	public RajuConnection(String propertiesFile) throws FileNotFoundException, InvalidPropertiesFormatException, IOException {
		super();
		setProperties(propertiesFile);
		instance=this;
	}


	public static RajuConnection getInstance() {
		return instance;
	}


	public RajuConnection() {
		super();
	}

public void close() throws SQLException{
	conn.close();
}
	
	private void setProperties(String fileName) throws FileNotFoundException,
	IOException,	InvalidPropertiesFormatException {
		this.prop = new Properties();
		FileInputStream fis = new FileInputStream(fileName);
		prop.loadFromXML(fis);

		this.dbms = this.prop.getProperty("dbms");
		this.jarFile = this.prop.getProperty("jar_file");
		this.driver = this.prop.getProperty("driver");
		this.dbName = this.prop.getProperty("database_name");
		this.userName = this.prop.getProperty("user_name");
		this.password = this.prop.getProperty("password");
		this.serverName = this.prop.getProperty("server_name");
		this.portNumber = Integer.parseInt(this.prop.getProperty("port_number"));

		System.out.println("Set the following properties:");
		System.out.println("dbms: " + dbms);
		System.out.println("driver: " + driver);
		System.out.println("dbName: " + dbName);
		System.out.println("userName: " + userName);
		System.out.println("serverName: " + serverName);
		System.out.println("portNumber: " + portNumber);
	}

	public Connection getConn() throws SQLException, ClassNotFoundException{
		if(conn==null){
			return getConnectionToDatabase();
		}
		return conn;
	}

	public Connection getConnectionToDatabase() throws SQLException, ClassNotFoundException {
		{
			Properties connectionProps = new Properties();
			connectionProps.put("user", this.userName);
			connectionProps.put("password", this.password);

			// Using a driver manager:
			Class.forName(this.driver);

			if (this.dbms.equals("mysql")) {
				//		        DriverManager.registerDriver(new com.mysql.jdbc.Driver());
				conn =
					DriverManager.getConnection("jdbc:" + dbms + "://" + serverName +
							":" + portNumber + "/" + dbName,
							connectionProps);
				conn.setCatalog(this.dbName);
			} else if (this.dbms.equals("derby")) {
				//		        DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
				conn =
					DriverManager.getConnection("jdbc:" + dbms + ":" + dbName, connectionProps);
			} else if (this.dbms.equals("oracle")) {
				//			        DriverManager.registerDriver(new org.apache.derby.jdbc.EmbeddedDriver());
				conn =
					DriverManager.getConnection("jdbc:" + dbms + ":thin:@" + serverName+":"+ portNumber + ":" + dbName, connectionProps);
			}
			System.out.println("Connected to database");
			return conn;
		}
	}

	public static void main(String[] args) throws FileNotFoundException, InvalidPropertiesFormatException, IOException, SQLException, ClassNotFoundException {
		RajuConnection rajuConnection=new RajuConnection();
		rajuConnection.setProperties("properties/oracle-properties.xml");
		Connection conn=rajuConnection.getConnectionToDatabase();
		conn.close();
		
	}

}
